/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include "AVR/avr-common.h"
#include    "rtos-wsn.h"

/*
extern void delay_ticks (int);
extern void CONTROL_DEMOSRC_STDinit(void);
extern void CONTROL_DEMOSRC_STDstart(void);
extern void CONTROL_DEMOSRC_STDstop(void);

extern void CONTROL_BLINKTEST_INinit(uint16, uint16);
extern void CONTROL_BLINKTEST_INstart(uint16);
extern void CONTROL_BLINKTEST_INstop(uint16);
*/

#ifdef _Debug_thread_

extern void CONTROL_THREADEVENT_TEINinit(uint16, uint16);
extern void CONTROL_THREADEVENT_TEINstart(uint16);
extern void CONTROL_THREADEVENT_TEINstop(uint16);

#define BLINK_TEST			1
#define BLINK_TO_RADIO  	2
#define THREAD_AND_EVENT	4
#endif

volatile uint16 test_state = 0;
uint8 wsn_app_flag = 0;

void usart0_init(void)
{
	/*set baud rate = 115200*/
	UBRR0H = 0x00;
	UBRR0L = 0x08;	
	UCSR0A |= BIT_SET(U2X0);	  
	/* TX0 output*/
	DDRE |= BIT_SET(PE1); 
	/* Enable receiver ,transmitter , RX_INT*/
	UCSR0B |= BIT_SET(TXEN0) | BIT_SET(RXEN0) | BIT_SET(RXCIE0);
	/* Set frame format:no Parity, 8data, 1stop bit */
	UCSR0C |= BIT_SET(UCSZ01)|BIT_SET(UCSZ00);

}


/* USART0 RECV ISR */
//ISR(USART0_RX_vect, ISR_BLOCK)
static uint8 usart_value;
void isr_usart0_rx(void)
{
	uint8 value;

	value = UDR0;	/* receive a byte */
	usart_value = value;
	wsn_LeaveCritical();
	switch(value)
	{
	case '0':	// stop all
/*
		if(test_state & BLINK_TEST){
			test_state &= ~BLINK_TEST;	
			CONTROL_BLINKTEST_INstop(0);//CONTROL_IN_IN(stop)->WSN_StopTimer
		}
		if(test_state & BLINK_TO_RADIO){
			test_state &= ~BLINK_TO_RADIO;	
			CONTROL_DEMOSRC_STDstop();//CONTROL_IN_STD(stop)
		}*/
#ifdef _Debug_thread_		
		if(test_state & THREAD_AND_EVENT){
			test_state &= ~THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINstop(0);//CONTROL_IN_TEIN(stop)->WSN_ClearEvent
		}
#endif
		wsn_app_flag = 0;
		break;
/*
	case '1':	// start all
		if((test_state & BLINK_TEST) == 0){
			test_state |= BLINK_TEST; // blink test
			CONTROL_BLINKTEST_INinit(0, 0);//CONTROL_IN_IN(init)->WSN_InitTimer
			CONTROL_BLINKTEST_INstart(0); //CONTROL_IN_IN(start)->WSN_StartTimer
		}
		if((test_state & BLINK_TO_RADIO) == 0){
			test_state |= BLINK_TO_RADIO;
			CONTROL_DEMOSRC_STDinit();//CONTROL_IN_STD(init)
			CONTROL_DEMOSRC_STDstart();//CONTROL_IN_STD(start)
		}
		if((test_state & THREAD_AND_EVENT) == 0){
			test_state |= THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINstart(0);////CONTROL_IN_TEIN(start)->WSN_SetEvent
		}

		wsn_app_flag = 1;

		break;

	case '2':
		if((test_state & BLINK_TEST) == 0){
			test_state |= BLINK_TEST; // blink test
			CONTROL_BLINKTEST_INinit(0, 0);
			CONTROL_BLINKTEST_INstart(0);
		}
		break;

	case '3':					// blink to radio
		if((test_state & BLINK_TO_RADIO) == 0){
			test_state |= BLINK_TO_RADIO;
			CONTROL_DEMOSRC_STDinit();
			CONTROL_DEMOSRC_STDstart();
		}
		break;
	*/
#ifdef _Debug_thread_

	case '4':					// thread & event test
		if((test_state & THREAD_AND_EVENT) == 0){
			test_state |= THREAD_AND_EVENT; // blink test
			CONTROL_THREADEVENT_TEINinit(0, 0);
			CONTROL_THREADEVENT_TEINstart(0);
		}
		break;
#endif
	case '5':					// app test
		wsn_app_flag = 1;
		break;

	default:
		return;
	}	

}


/* output */
int dbgputs (void * f, char * str, unsigned int count)
{
  if (count == 0) {
			return 0;
    }
	
	while(count--)
	{
		while( !(UCSR0A & (1<<UDRE0)) ) /* USART data register empty */
			;
		UDR0 = *str;
		str++;

	}
	return 0;
}

unsigned char console_check_input(void)
{
	return usart_value;
}

void console_input_clear(void)
{
	wsn_EnterCritical();
	usart_value = 0;
	wsn_LeaveCritical();
}

